package com.ihd.ihardware.view.enter.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityDevicesBinding;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.enter.viewModel.DevicesViewModel;
import com.ihd.ihardware.view.lock.view.LockNoBindingActivity;
import com.ihd.ihardware.view.scooter.view.SCNoBindingActivity;
import com.ihd.ihardware.view.tzc.main.view.BalanceActivity;
import com.ihd.ihardware.view.tzc.me.view.DeviceActivity;
import com.ihd.ihardware.view.tzc.me.view.PerInfoActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;

public class DevicesActivity extends BaseActivity<ActivityDevicesBinding, DevicesViewModel> {
    private List<DeviceListRes.DataBean.ListBean> ddls;
    private UserInfoRes.DataBean uInfo;
    private DeviceListRes.DataBean.ListBean tzcddl;
    private DeviceListRes.DataBean.ListBean hbcddl;
    private List<DeviceListRes.DataBean.ListBean> lysddl = new ArrayList<>();
    private MDialog alert;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();

        uInfo = (UserInfoRes.DataBean) getIntent().getSerializableExtra("userinfo");

        binding.mtitlebar.setLeftImageResource(R.drawable.back);
        binding.mtitlebar.setLeftBack(this);
        binding.mtitlebar.setTitle("选择智能硬件");

        setRx(AppConstans.UNBIND, new BaseConsumer<DeviceListRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(DeviceListRes mLiveData) {
                DialogUtils.successDialog(DevicesActivity.this, MDialog.DG_TYPE.SUCCESS, R.drawable.toasticon_suc, "解除成功");
                SPUtils.putString(AppConstans.DEVICES, new Gson().toJson(mLiveData.getData().getList(), new TypeToken<List<DeviceListRes.DataBean.ListBean>>() {
                }.getType()));
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        updateStatus();
                    }
                }, 1000);
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(DevicesActivity.this, error.getMessage());
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateStatus();
    }

    void updateStatus() {
        lysddl.clear();
        tzcddl = null;
        hbcddl = null;
        String temp = SPUtils.getString(AppConstans.DEVICES, "");
        if (!TextUtils.isEmpty(temp)) {
            ddls = new Gson().fromJson(temp, new TypeToken<List<DeviceListRes.DataBean.ListBean>>() {
            }.getType());
            for (DeviceListRes.DataBean.ListBean d : ddls) {
                if ("0".equals(d.getType()) || "1".equals(d.getType()))
                    tzcddl = d;
                if ("2".equals(d.getType()) || "3".equals(d.getType()))
                    lysddl.add(d);
                if ("4".equals(d.getType()))
                    hbcddl = d;
            }
        } else
            ddls = new ArrayList<>();

        if (tzcddl != null) {
            binding.TZCstatus.setText("已绑定");
            binding.TZCstatus.setBackgroundResource(R.drawable.corners_line_w_4);
            binding.TZCicon.setImageResource(R.drawable.tindex_tzc_lined);
            binding.TZC.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent mIntent = new Intent(DevicesActivity.this, DeviceActivity.class);
                    mIntent.putExtra("device", tzcddl);
                    startActivity(mIntent);
                }
            });
        } else {
            binding.TZCstatus.setText("去绑定");
            binding.TZCstatus.setBackgroundResource(R.drawable.corners_b_10p);
            binding.TZCicon.setImageResource(R.drawable.tindex_tzc_unline);
            binding.TZC.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (uInfo == null) {
                        Intent mIntent = new Intent(DevicesActivity.this, PerInfoActivity.class);
                        startActivity(mIntent);
                        finish();
                    } else {
                        if (TextUtils.isEmpty(uInfo.getWeight())) {
                            Intent mIntent = new Intent(DevicesActivity.this, PerInfoActivity.class);
                            mIntent.putExtra("userinfo", uInfo);
                            startActivity(mIntent);
                            finish();
                        } else {
                            Intent mIntent = new Intent(DevicesActivity.this, BalanceActivity.class);
                            startActivity(mIntent);
                            finish();
                        }
                    }
                }
            });
        }

        if (hbcddl != null) {
            binding.QXstatus.setText("已绑定");
            binding.QXstatus.setBackgroundResource(R.drawable.corners_line_w_4);
            binding.ZNQXicon.setImageResource(R.drawable.tindex_znqx_lined);
            binding.ZNQX.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alert = DialogUtils.alertDialog(DevicesActivity.this, MDialog.DG_TYPE.ALERT, "解除绑定设备后将无法同步数据，确认解绑?", "我再想想", "确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            alert.cancel();
                        }
                    }, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            alert.cancel();
                            viewModel.unbind(hbcddl.getId());
                        }
                    });
                }
            });
        } else {
            binding.QXstatus.setText("去绑定");
            binding.QXstatus.setBackgroundResource(R.drawable.corners_b_10p);
            binding.ZNQXicon.setImageResource(R.drawable.tindex_znqx_unline);
            binding.ZNQX.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent mIntent = new Intent(DevicesActivity.this, SCNoBindingActivity.class);
                    startActivity(mIntent);
                    finish();
                }
            });
        }

        if (lysddl.size() != 0) {
            binding.LYSstatus.setText("已绑定");
            binding.LYSstatus.setBackgroundResource(R.drawable.corners_line_w_4);
            binding.LYSicon.setImageResource(R.drawable.tindex_lys_lined);
            binding.LYS.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent mIntent = new Intent(DevicesActivity.this, LockManageActivity.class);
                    mIntent.putExtra("devices", (Serializable) lysddl);
                    startActivity(mIntent);
                }
            });
        } else {
            binding.LYSstatus.setText("去绑定");
            binding.LYSstatus.setBackgroundResource(R.drawable.corners_b_10p);
            binding.LYSicon.setImageResource(R.drawable.tindex_lys_unline);
            binding.LYS.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent mIntent = new Intent(DevicesActivity.this, LockNoBindingActivity.class);
                    startActivity(mIntent);
                    finish();
                }
            });
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_devices;
    }

    @Override
    protected Class getViewModelClass() {
        return DevicesViewModel.class;
    }

}
